﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataTransferObject;
using BusinessLogicLayer;
using DevComponents.DotNetBar;

namespace PKSoftManagement
{
    public partial class ChiTietBenhAn : UserControl
    {
        int idBN, idBA;
        bool isNew = false;
        BenhBanDau_NoiDungDieuTriBLL BLL;
        public ChiTietBenhAn(int idBN, int idBA)//load
        {
            InitializeComponent();
            this.idBA = idBA;
            this.idBN = idBN;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView2.AutoGenerateColumns = false;
            BLL = new BenhBanDau_NoiDungDieuTriBLL();
            LoadInfo();
        }
        public ChiTietBenhAn(int idBN)//create new
        {
            InitializeComponent();
            BLL = new BenhBanDau_NoiDungDieuTriBLL();
            isNew = true;
            this.idBN = idBN;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView2.AutoGenerateColumns = false;
        }
        #region
        private void DoDieuTri()
        {
            var list = new HoSoBenhNhanBLL().GetDieuTri(idBN);
            dataGridView1.DataSource = list.ToList();
        }
        private void DoThongTinThuoc()
        {
            var list = new HoSoBenhNhanBLL().GetThongTinThuoc(idBN);
            dataGridView2.DataSource = list.ToList();
        }
        private void saveBA_Click(object sender, EventArgs e)
        {
            if (isNew)
            {
                new BenhBanDau_NoiDungDieuTriBLL().Insert(new BenhBanDau_NoiDungDieuTriDto()
                {
                    IdBenhNhan = idBN,
                    HuongDieuTri = huongDieuTri.Text,
                    GhiChu = ghiChutxt.Text,
                    Date = ngayDieuTri.Value,
                    ChuanDoanBenh = chuanDoan.Text
                });
            }
            else
            {
                new BenhBanDau_NoiDungDieuTriBLL().Update(new BenhBanDau_NoiDungDieuTriDto(){
                    Id=this.idBA,
                    ChuanDoanBenh=chuanDoan.Text,
                    HuongDieuTri=huongDieuTri.Text,
                    GhiChu=ghiChutxt.Text,
                    Date=ngayDieuTri.Value
                });
            }
            MessageBox.Show("Đã Lưu Thành Công");
        }
        private void LoadInfo()
        {
            DoDieuTri();
            DoThongTinThuoc();
            maBA.Text = idBA.ToString();
            maBN.Text = idBN.ToString();
            tenBN.Text = new HoSoBenhNhanBLL().Get(idBN).Select(e => e.Ten).FirstOrDefault();
            chuanDoan.Text = BLL.Get(idBA).Select(e => e.ChuanDoanBenh).FirstOrDefault();
            //ngayBenh.Value=;
            var t = BLL.Get(idBA).Select(e => e.Date).FirstOrDefault();
            ngayDieuTri.Value = t.HasValue ? t.Value : DateTime.Now;
            huongDieuTri.Text = BLL.Get(idBA).Select(e => e.HuongDieuTri).FirstOrDefault();
            soNgay.Value = 0;//tru ngay Date- old
            //bacSi.Items= //lay item tu danh sach bs
            ghiChutxt.Text = BLL.Get(idBA).Select(e => e.GhiChu).FirstOrDefault();
        }
        #endregion
    }
}
